﻿using IOC.Desgin;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace IOC.Builder
{
   public class RegisterEntity
    {
		public Type RegistType { get; set; }

		public Type RealType { get; set; }

		public string Name { get; set; }

		public bool IsEnableIntercept { get; set; }

		public bool IsInterceptAllMethod { get; set; }

		public Type InterceptType { get; set; }

		public object EntityInstance { get; set; }

		public RegisterEntity(Type RegistType)
		{
			this.RegistType = RegistType;
			RealType = RegistType;
			Name = RegistType.Name;
			IsEnableIntercept = false;
			IsInterceptAllMethod = false;
		}

		public RegisterEntity As<T>()
		{
			RealType = typeof(T);
			Name = RealType.Name;
			return this;
		}

		public RegisterEntity Named<T>(string Name)
		{
			RealType = typeof(T);
			this.Name = Name;
			return this;
		}

		public RegisterEntity AsImplementedInterfaces()
		{
			Type[] BaseinterfaceList = RegistType.GetInterfaces();
			if (BaseinterfaceList.Count() > 0)
			{
				RealType = BaseinterfaceList[0];
				Name = RealType.Name;
				return this;
			}
			throw new NoBaseInterfaceException();
		}

		public RegisterEntity EnableInterfaceInterceptors()
		{
			IsEnableIntercept = true;
			IsInterceptAllMethod = true;
			return this;
		}

		public RegisterEntity InterceptedBy<T>() where T : IIntercept
		{
			InterceptType = typeof(T);
			return this;
		}

		public RegisterEntity UseAttributeIntercept()
		{
			IsInterceptAllMethod = false;
			return this;
		}
	}
}
